package com.ccc.controller;


import com.ccc.entity.TeaNotif;
import com.ccc.mapper.TeaNotifMapper;
import com.ccc.service.ITeaNotifService;
import com.ccc.utils.JwtUtil;
import com.ccc.utils.ResponseResult;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ccc
 * @since 2021-12-27
 */
@Controller
public class TeaNotifController {

    @Autowired
    TeaNotifMapper teaNotifMapper;

    /**
     * 教师添加通知
     *
     * @param teaNotif
     * @param request
     * @return
     */
    @PostMapping("/teacher/addNotification")
    @PreAuthorize("hasAuthority('teacher')")
    @ResponseBody
    public ResponseResult addNotification(@RequestBody TeaNotif teaNotif, HttpServletRequest request) {
        String token = request.getHeader("token");
        String username;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            username = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400, "token Error!");
        }
        teaNotifMapper.addNotification(username, teaNotif.getContent());
        return new ResponseResult(200, "success");
    }

    /**
     * 任意身份请求通知表，返回全部的通知
     *
     * @return
     */
    @GetMapping("/user/Notification")
    @PreAuthorize("hasAnyAuthority('student','teacher')")
    @ResponseBody
    public ResponseResult getNotification() {
        return new ResponseResult(200, "success", teaNotifMapper.NotificationList());
    }
}
